package com.playball.lottery.frm.ads;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.frm.buyhall.BDActivity;
import com.playball.lottery.frm.buyhall.CQSSCActivity;
import com.playball.lottery.frm.buyhall.CtzqRx9Activity;
import com.playball.lottery.frm.buyhall.CtzqSfcActivity;
import com.playball.lottery.frm.buyhall.DltxhActivity;
import com.playball.lottery.frm.buyhall.ElvSelFiveActivity;
import com.playball.lottery.frm.buyhall.ElvYunDJActivity;
import com.playball.lottery.frm.buyhall.Fc3dxhActivity;
import com.playball.lottery.frm.buyhall.JcBasketballActivity;
import com.playball.lottery.frm.buyhall.JcfootballActivity;
import com.playball.lottery.frm.buyhall.Kuai3Activity;
import com.playball.lottery.frm.buyhall.N7lxhActivity;
import com.playball.lottery.frm.buyhall.N7xxhActivity;
import com.playball.lottery.frm.buyhall.Pailie3xhActivity;
import com.playball.lottery.frm.buyhall.Pailie5xhActivity;
import com.playball.lottery.frm.buyhall.SsqxhActivity;
import com.playball.lottery.frm.yhzx.JddRechargeListActivity;
import com.playball.lottery.frm.yhzx.LoginActivity;
import com.playball.lottery.util.Logger;
import com.playball.lottery.util.LotConstants;


public class AdActivity extends BaseActivity implements LotConstants {

	// 顶部菜单栏
	private Button top_title_back;
	private TextView top_title_info;
	private Button top_title_refres;
	private WebView webView;

	private Context mActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ad);

		mActivity = this;

		initView();
		initData();
	}

	/**
	 * 初始化组件
	 */
	private void initView() {
		top_title_back = (Button) findViewById(R.id.top_title_back);
		top_title_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		top_title_info = (TextView) findViewById(R.id.top_title_info);
		top_title_refres = (Button) findViewById(R.id.top_title_refres);
		top_title_refres.setVisibility(View.INVISIBLE);
		webView = (WebView) findViewById(R.id.web);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		webView.addJavascriptInterface(new CallByWeb(), "callToAndroid");
		webView.addJavascriptInterface(new CallByWeb(), "callToApp");
	}

	public void onResume() {
		super.onResume();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		Intent intent = getIntent();
		String title = intent.getStringExtra("ad_title");
		String url = intent.getStringExtra("ad_url");

		if (url != null) {
			if (url.toLowerCase().indexOf("http://") != 0) {
				url = "http://" + url;
			}
		}

		/* 测试地址 */
		// url = "http://duba.wap.jiangduoduo.com/go/140610/index.html";
		// url = "http://duba.wap.jiangduoduo.com/go/140612/index.html";

		Logger.e("url", url);

		top_title_info.setText(title);
		webView.loadUrl(url);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
			webView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public class CallByWeb {
		/**
		 * type 跳转类型，字符型 1|金额 充值：例如web页面要求充值100参数为1|100 2|彩种ID
		 * 跳转购彩：例如跳转竞彩足球购彩页面2|90
		 * 
		 * 
		 * js调用方法：window.callToApp.callByWebView(type);
		 */
		public void callByWebView(final String type) {
			runOnUiThread(new Runnable() {
				public void run() {
					// 根据args处理结果冰跳转
					String[] paras = type.split("\\|");
					if (paras.length == 2) {
						Intent intent = new Intent();
						if ("1".equals(paras[0])) {
							intent.putExtra("recharge", paras[1]);
							if (ApplicationData.getInstance().isUserLogin()) {
								intent.setClass(mActivity, JddRechargeListActivity.class);
							} else {
								intent.setClass(mActivity, LoginActivity.class);
							}
						} else if ("2".equals(paras[0])) {
							switch (Integer.parseInt(paras[1])) {
							case LOT_ID_SSQ:
								intent.setClass(mActivity, SsqxhActivity.class);
								break;
							case LOT_ID_DLT:
								intent.setClass(mActivity, DltxhActivity.class);
								break;
							case LOT_ID_11X5:
								intent.setClass(mActivity, ElvSelFiveActivity.class);
								break;
							case LOT_ID_JCZQ:
								intent.setClass(mActivity, JcfootballActivity.class);
								break;
							case LOT_ID_BD:
								intent.setClass(mActivity, BDActivity.class);
								break;
							case LOT_ID_JCLQ:
								intent.setClass(mActivity, JcBasketballActivity.class);
								break;
							case LOT_ID_ZQ_SFC:
								intent.setClass(mActivity, CtzqSfcActivity.class);
								break;
							case LOT_ID_ZQ_RX9:
								intent.setClass(mActivity, CtzqRx9Activity.class);
								break;
							case LOT_ID_FC3D:
								intent.setClass(mActivity, Fc3dxhActivity.class);
								break;
							case LOT_ID_QLC:
								intent.setClass(mActivity, N7lxhActivity.class);
								break;
							case LOT_ID_PL3:
								intent.setClass(mActivity, Pailie3xhActivity.class);
								break;
							case LOT_ID_PL5:
								intent.setClass(mActivity, Pailie5xhActivity.class);
								break;
							case LOT_ID_QXC:
								intent.setClass(mActivity, N7xxhActivity.class);
								break;
							case LOT_ID_CQSSC:
								intent.setClass(mActivity, CQSSCActivity.class);
								break;
							case LOT_ID_11YDJ:
								intent.setClass(mActivity, ElvYunDJActivity.class);
								break;
							case LOT_ID_KUAI3:
								intent.setClass(mActivity, Kuai3Activity.class);
								break;
							}
						} else if ("6".equals(paras[0])) {
							intent.putExtra("recharge", paras[1].split("_")[0]);
							intent.putExtra("showBalance", paras[1].split("_")[1]);
							if (ApplicationData.getInstance().isUserLogin()) {
								intent.setClass(mActivity, JddRechargeListActivity.class);
								intent.putExtra("adEnabled", "1");
								// findViewById(R.id.et_recharge_money).setEnabled(false);

							} else {
								intent.setClass(mActivity, LoginActivity.class);
							}
						}
						startActivity(intent);
					}
				}
			});
		}
	}

}
